<template>
  <!-- 客户留存分析 -->
  <div class="components-customer-profile">
    <!-- 标题栏 -->
    <div class="data-analysis title-bar">
      <div class="module-title">
        <div class="tit-lf">
          {{ $t('dataAnalysis.customerRetentionAnalysis') }}
        </div>
        <div class="tit-rg">
          <div class="statistics-time">
            <span>{{ $t('dataAnalysis.statisticalTime') }}：</span>
            <el-select
              v-model="rangeValue"
              :placeholder="$t('tip.select')"
              clearable
              @change="setDateRange(rangeValue)"
            >
              <el-option
                v-for="item in rangeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </div>
          <div class="date-display">
            {{ dateRange[0] }}{{ $t('date.tip') }}{{ dateRange[1] }}
          </div>
        </div>
      </div>
    </div>
    <!-- 选项卡 -->
    <el-tabs
      v-model="retainType"
      class="tabs"
      type="card"
    >
      <el-tab-pane
        :label="$t('dataAnalysis.dealRetention')"
        name="2"
      />
    </el-tabs>

    <!-- 表格 -->
    <div class="table-box">
      <div class="table">
        <el-table
          :data="dataList"
          header-cell-class-name="table-header"
          border
        >
          <el-table-column
            v-if="retainType==='2'"
            fixed
            prop="currentMonth"
            :label="$t('dataAnalysis.dealTime')"
            width="180"
          />
          <el-table-column
            v-if="retainType==='2'"
            prop="newCustomers"
            :label="$t('dataAnalysis.newCustomer')"
            width="180"
          />
          <el-table-column
            v-if="retainType==='1'"
            fixed
            prop="currentMonth"
            :label="$t('dataAnalysis.accessTime')"
            width="180"
          />
          <el-table-column
            v-if="retainType==='1'"
            prop="newCustomers"
            :label="$t('dataAnalysis.newCustomer')"
            width="180"
          />

          <el-table-column
            v-if="1<=colLength"
            :label="$t('dataAnalysis.retained1')"
          >
            <template #default="scope">
              <div>
                <p>{{ scope.row.firstMonthRemain }}</p>
                <p v-if="scope.row.firstMonthRemainRate !== null">
                  {{ scope.row.firstMonthRemainRate }}%
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="2<=colLength"
            :label="$t('dataAnalysis.retained2')"
          >
            <template #default="scope">
              <div>
                <p>{{ scope.row.secondMonthRemain }}</p>
                <p v-if="scope.row.secondMonthRemainRate !== null">
                  {{ scope.row.secondMonthRemainRate }}%
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="3<=colLength"
            :label="$t('dataAnalysis.retained3')"
          >
            <template #default="scope">
              <div>
                <p>{{ scope.row.thirdMonthRemain }}</p>
                <p v-if="scope.row.thirdMonthRemain !== null">
                  {{ scope.row.thirdMonthRemainRate }}%
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="4<=colLength"
            :label="$t('dataAnalysis.retained4')"
          >
            <template #default="scope">
              <div>
                <p>{{ scope.row.fourthMonthRemain }}</p>
                <p v-if="scope.row.fourthMonthRemain !== null">
                  {{ scope.row.fourthMonthRemainRate }}%
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="5<=colLength"
            :label="$t('dataAnalysis.retained5')"
          >
            <template #default="scope">
              <div>
                <p>{{ scope.row.fifthMonthRemain }}</p>
                <p v-if="scope.row.fifthMonthRemain !== null">
                  {{ scope.row.fifthMonthRemainRate }}%
                </p>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="6<=colLength"
            :label="$t('dataAnalysis.retained6')"
          >
            <template #default="scope">
              <div>
                <p>{{ scope.row.sixthMonthRemain }}</p>
                <p v-if="scope.row.sixthMonthRemainRate !== null">
                  {{ scope.row.sixthMonthRemainRate }}%
                </p>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup>
import moment from 'moment'

// 时间范围
const rangeOptions = [
  {
    label: $t('dataAnalysis.last90D'),
    value: 2
  },
  {
    label: $t('dataAnalysis.last180D'),
    value: 3
  },
  {
    label: $t('dataAnalysis.last365D'),
    value: 4
  }
]

const colLength = ref(0)
const dateRange = ref([])
const retainType = ref('2') // 1访问留存  2成交留
watch([() => dateRange.value, () => retainType.value], () => {
  // 客户留存分析后台数据
  getMemberRetentionData()
})

const rangeValue = ref(2) // 1最近1个月 2最近3个月 3最近6个月 4最近1年
onMounted(() => {
  setDateRange(rangeValue.value)
})

const dataList = ref([])
const getMemberRetentionData = () => {
  if (rangeValue.value === 1) {
    colLength.value = 1
  } else if (rangeValue.value === 2) {
    colLength.value = 2
  } else if (rangeValue.value === 3) {
    colLength.value = 5
  } else if (rangeValue.value === 4) {
    colLength.value = 6
  }
  const param = {
    dateType: rangeValue.value, // 1最近1个月 2最近3个月 3最近6个月 4最近1年
    retainType: retainType.value, // 1访问留存  2成交留存
    dateRetainType: 1, // 1月留存 2周留存 暂时不统计周留存
    startTime: dateRange.value[0].indexOf(':') !== -1 ? dateRange.value[0] : dateRange.value[0] + ' 00:00:00', // yyyy-MM-dd HH:mm:ss
    endTime: dateRange.value[1].indexOf(':') !== -1 ? dateRange.value[1] : dateRange.value[1] + ' 23:59:59' // yyyy-MM-dd HH:mm:ss
  }
  http({
    url: http.adornUrl('/platform/flowCustomerAnalysis/getCustomerRetained'),
    method: 'get',
    params: http.adornParams(param)
  }).then(data => {
    data = data.data
    if (data) {
      dataList.value = data
    } else {
      dataList.value = []
    }
  })
}
const setDateRange = (val) => {
  let startDay = null
  let endDay = null
  if (val === 2) {
    startDay = -90
    endDay = 0
  }
  if (val === 3) {
    startDay = -180
    endDay = 0
  }
  if (val === 4) {
    startDay = -360
    endDay = 0
  }
  // 开始时间
  const startTime = moment().add(startDay, 'days').format('L')
  // 结束时间
  const endTime = moment().add(endDay, 'days').format('L')
  nextTick(() => {
    dateRange.value = [startTime, endTime]
  })
}
</script>

<style lang="scss" scoped>
.components-customer-profile {
  // 数据分析板块标题栏
  .data-analysis.title-bar {
    // 板块标题栏
    .module-title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      background: #f7f8fa;
      padding: 16px 10px;
      .tit-lf {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        font-size: 17px;
        font-weight: bold;
        padding-left: 6px;
        color: #333;
      }
      .tit-lf::before {
        display: block;
        content: '';
        width: 3px;
        height: 16px;
        background: #155bd4;
        margin-right: 5px;
        border-radius: 8px;
      }
      .tit-rg {
        display: flex;
        align-items: center;
        font-size: 14px;
        .statistics-time {
          margin-right: 10px;
        }
        .date-display {
          font-size: 12px;
          color: #666;
        }
      }
    }
  }
  // 选项卡
  .tabs {
    margin-top: 20px;
    :deep(.el-tabs__item.is-top) {
      background: #F7F8FA;
      border-bottom: 1px solid #E4E7ED;
    }
    :deep(.el-tabs__item.is-top.is-active) {
      background: #fff;
      border-bottom: 1px solid #fff;
    }
  }
  // 表格
  .table-box {
    margin-bottom: 20px;
    margin-top: 15px;
    font-size: 14px;

    p{
      margin:0;
    }
  }
}
</style>
